package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()) {
            for(DishFlavor f : flavors) {
                f.setDishId(dishId);
                dishFlavorMapper.insert(f);
            }
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        String name = dishPageQueryDTO.getName();
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        Integer page = dishPageQueryDTO.getPage();
        Integer pageSize = dishPageQueryDTO.getPageSize();
        Integer status = dishPageQueryDTO.getStatus();
        Integer categoryId = dishPageQueryDTO.getCategoryId();

        if(name != null && !name.isEmpty()) {
            page = 1;
            queryWrapper.like("name", name);
        }
        if(categoryId != null) {
            page = 1;
            queryWrapper.eq("category_id", categoryId);
        }
        if(status != null) {
            page = 1;
            queryWrapper.eq("status", status);
        }
        Page<Dish> pDish = new Page<>(page, pageSize);
        Long count = dishMapper.selectCount(queryWrapper);
        List<Dish> listDish = dishMapper.selectPage(pDish, queryWrapper).getRecords();
        List<DishVO> dishVOList = new ArrayList<>();
        for(Dish d : listDish) {
            DishVO dishVO = new DishVO();
            QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id", d.getId());
            List<DishFlavor> dishFlavor = dishFlavorMapper.selectList(queryWrapper1);
            BeanUtils.copyProperties(d, dishVO);
            dishVO.setCategoryName(categoryMapper.selectById(d.getCategoryId()).getName());
            dishVO.setFlavors(dishFlavor);
            dishVOList.add(dishVO);
        }
        Page<DishVO> dishVOPage = new Page<>();
        dishVOPage.setRecords(dishVOList);

        return new PageResult(count, dishVOPage.getRecords());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for(Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        for(Long id : ids) {
            QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dish_id", id);
            if(setmealDishMapper.selectCount(queryWrapper) > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        dishMapper.deleteBatchIds(ids);
        for(Long id : ids) {
            QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dish_id", id);
            dishFlavorMapper.delete(queryWrapper1);
        }
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setCategoryName(categoryMapper.selectById(dish.getCategoryId()).getName());
        QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dish_id", id);
        List<DishFlavor> flavors = dishFlavorMapper.selectList(queryWrapper1);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);

        QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dish_id", dish.getId());
        dishFlavorMapper.delete(queryWrapper1);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()) {
            for(DishFlavor f : flavors) {
                f.setDishId(dishDTO.getId());
                dishFlavorMapper.insert(f);
            }
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.selectList(new QueryWrapper<Dish>().eq("category_id", categoryId));
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = dishMapper.selectById(id);
        dish.setStatus(status);
        dishMapper.updateById(dish);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        if(dish.getName() != null) {
            queryWrapper.like("name", dish.getName());
        }
        if(dish.getCategoryId() != null) {
            queryWrapper.eq("category_id", dish.getCategoryId());
        }
        if(dish.getStatus() != null) {
            queryWrapper.eq("status", dish.getStatus());
        }
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        System.out.println(dishList);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dish_id", d.getId());
            List<DishFlavor> flavors = dishFlavorMapper.selectList(queryWrapper1);

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
